<?php

namespace app\common\model;

use think\Model;

class ActivityOrder extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'send_out_images_url',
    ];

    public function getStatusTextAttr($value, $data)
    {
        $status = [1 => '未发货', 2 => '已发货', 3 => '完成'];
        return $status[$data['status']] ?? '-';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        return date('Y/m/d H:i:s', $data['createtime']);
    }

    public function getSendOutImagesUrlAttr($value, $data)
    {
        $value = $data['send_out_images'] ?? '';
        if ($value) {
            $send_out_images = explode(',', $value);
            foreach ($send_out_images as &$v) {
                $v = [
                    'url' => cdnurl($v, true),
                ];
            }
            return $send_out_images;
        } else {
            return [];
        }
    }

    public function activity()
    {
        return $this->belongsTo('Activity', 'activity_id', 'id');
    }

    public function shop()
    {
        return $this->hasOne('User', 'id', 'shop_id')->field('id,nickname');
    }

    public function user()
    {
        return $this->hasOne('User', 'id', 'buy_id')->field('id,nickname,mobile');
    }

    public function dre()
    {
        return $this->hasOne('User', 'id', 'dre_id')->field('id,nickname');
    }

}
